/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.itver.graphics.controller;

/**
 *
 * @author Karo
 */

import java.awt.event.*;
import java.awt.AWTEvent;
import java.util.Enumeration;
import javax.media.j3d.*;
import org.itver.graphics.model.Camera;

/**
 * This class is a simple behavior that invokes the KeyNavigator
 * to modify the view platform transform.
 */
/****************************************************
 * COMPONER ESTE CÓDIGO ¬¬ URGE
 * @author Karo
 */
public class CameraKeyBehavior extends Behavior {

    private WakeupOnAWTEvent wakeupOne = null; //Class that specifies a Behavior wakeup when a specific AWT event occurs.
    private WakeupCriterion[] wakeupArray = new WakeupCriterion[1]; //
    private WakeupCondition wakeupCondition = null;
    private TransformGroup targetTG;
    private TransformGroupManipulator tgManipulator;
    private Camera camera;

    public CameraKeyBehavior(TransformGroup targetTg) {
//        this.camera = camera;
        this.targetTG = targetTg;
        try {
            targetTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            targetTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        } catch (Exception e) {
        }
        tgManipulator = new TransformGroupManipulator(targetTg);
        wakeupOne = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
        wakeupArray[0] = wakeupOne;
        wakeupCondition = new WakeupOr(wakeupArray);
    }

    /**
     *  Override Behavior's initialize method to setup wakeup criteria.
     */
    @Override
    public void initialize() {
        // Establish initial wakeup criteria
        wakeupOn(wakeupCondition);
    }

    /**
     *  Override Behavior's stimulus method to handle the event.
     */
    @Override
    public void processStimulus(Enumeration criteria) {
        WakeupOnAWTEvent ev;
        WakeupCriterion genericEvt;
        AWTEvent[] events;

        while (criteria.hasMoreElements()) {
            genericEvt = (WakeupCriterion) criteria.nextElement();

            if (genericEvt instanceof WakeupOnAWTEvent) {
                ev = (WakeupOnAWTEvent) genericEvt;
                events = ev.getAWTEvent();
                processAWTEvent(events);
            }
        }

        // Set wakeup criteria for next time
        wakeupOn(wakeupCondition);
    }

    /**
     *  Process a keyboard event
     */
    private void processAWTEvent(AWTEvent[] events) {

        for (int n = 0; n < events.length; n++) {
            if (events[n] instanceof KeyEvent) {
                KeyEvent eventKey = (KeyEvent) events[n];
                if (eventKey.getID() == KeyEvent.KEY_PRESSED) {
                    int keyCode = eventKey.getKeyCode();
                    int keyChar = eventKey.getKeyChar();

//                    tgManipulator.getTransform();
                    switch (keyCode) {
                        case KeyEvent.VK_W:
                            tgManipulator.stepForward();
                            break;

                        case KeyEvent.VK_S:
                            tgManipulator.stepBackward();
                            break;

                        case KeyEvent.VK_SPACE:
                            tgManipulator.riseUp();
                            break;

                        case KeyEvent.VK_X:

                            tgManipulator.goDown();
                            break;

                        case KeyEvent.VK_A:
                            tgManipulator.turnLeft();
                             break;

                        case KeyEvent.VK_D:
                            tgManipulator.turnRight();
                            break;

                        case KeyEvent.VK_Q:
                            tgManipulator.stepLeftward();
                            break;

                        case KeyEvent.VK_E:
                            tgManipulator.stepRightward();
                            break;

                    }
//                    tgManipulator.setTransform();
                }
            }
        }
    }

    public TransformGroupManipulator getTransformGroupManipulator(){
        return tgManipulator;
    }
}
